import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { BinaryDecision } from "framework/FSM/DecisionTree/Decision";
import { PositionHelper } from "utils/position/PositionHelper";
import { UtilsHelper } from "utils/position/UtilsHelper";

 

/**
 * 是否已经靠近目标房间
*/
export class DecisionIsTeamReadyCombat <T extends IStateArg<ATeamCombat,ITask>> extends BinaryDecision<T>
{

    public testValue(arg:T): boolean
    {
         
            const movetarget =arg.entity.moveTarget();
            if(!movetarget)
            {
                return false;
            }

            // 准备进入战斗形态
            /**
             * 1.即将抵达房间
             * 2.已经在房间里
            */
            // 1.
            const anchor = arg.entity.getAnchor();
            arg.entity.log("info",`---------- 锚点:[${JSON.stringify(anchor)}] 目标:[${JSON.stringify(arg.entity.creeps.map(e=>e.name))}]    `);
            if(Game.map.getRoomLinearDistance(anchor.roomName, movetarget.pos.roomName)>1)
            {
                //global.Log.Info(`锚点:[${JSON.stringify(anchor)}] 目标:[${JSON.stringify(arg.entity.moveTarget.position)}]    assert1 房间距离大于1 line状态`);
                return false;
            }
            if(anchor.roomName == movetarget.pos.roomName)
            {
               // global.Log.Info(`锚点:[${JSON.stringify(anchor)}] 目标:[${JSON.stringify(arg.entity.moveTarget.position)}]   assert2 true 锚点抵达目标房间`);
                return true;
            }
            return this.isNearTargetRoom(anchor,movetarget.pos.roomName);
         
    }

    value: any;

    private getStepPosition(curpos:IRoomPosition,path:string,index:number,step:number)
    {
        let offset:IFVector2={x:0,y:0};
        for(let i=0;i<step && (index+i)<path.length;i++)
        {
            const d = UtilsHelper.getOffset(parseInt( path[index+i]) as DirectionConstant);
            offset.x+=d[0];
            offset.y+=d[1];
        }
        return PositionHelper.addPosition(curpos,offset)
    }

    private isNearTargetRoom(curpos:IRoomPosition,targetRoomName:string)
    {
        const nabrooms = Game.map.describeExits(curpos.roomName);
        for(const dir in nabrooms)
        {
            if(nabrooms[dir as ExitKey] == targetRoomName)
            {
                switch(dir as ExitKey)
                {
                    case "1":
                        if(curpos.y<4)
                        {
                            return true;
                        }
                        break;
                    case "3":
                        if(curpos.x>45)
                        {
                            return true;
                        }
                        break;
                    case "5":
                        if(curpos.y>45)
                        {
                            return true;
                        }
                        break;
                    case "7":
                        if(curpos.x<4)
                        {
                            return true;
                        }
                        break;
                }
                return false;
            }
        }
        return false;
    }
    public setValue(val: any)
    {
        this.value = val;
    }
}

